﻿Imports System.Xml
Public Class clsJRIProject
#Region "数据调整"
    ''' <summary>
    ''' JRI项目对于数据的调整
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub JustifyDataByJRI()
        '@JRI  PROC 特殊处理 开始
        '增加3个JRI特殊的初始化宏
        clsJRIProject.AddInitMacro()
        '@JRI  PROC 特殊处理 完了
        For Each mData As XmlElement In mDataNode.WorkStorageList
            '@JRI  PROC 特殊处理 开始
            'ISXPROC1关联的修改
            If mData.Name = "INCLUDE" And mData.GetAttribute("COPYNAME") = "ISXPROC1" Then
                mData.SetAttribute("COPYBOOKNAME", "ISXPPDP2")
                Dim parmXml As XmlElement = mXmlDocument.CreateElement("SINGLEDATA")
                parmXml.SetAttribute("NAME", "PRAM#PTR")
                parmXml.SetAttribute("TYPE", "PTR")
                mDataNode.LinkageList.Insert(0, parmXml)
            End If
        Next
    End Sub
    ''' <summary>
    ''' 添加JRI特殊的初始化MACRO
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub AddInitMacro()
        Dim XCOMMONXml As XmlElement = mXmlDocument.CreateElement("MACRO")
        If mIdentificationNode.IsMain Then
            XCOMMONXml.SetAttribute("COMMAND", "@MCOMMON.")
        Else
            XCOMMONXml.SetAttribute("COMMAND", "@SCOMMON.")
        End If
        mDataNode.AddData(XCOMMONXml)

        Dim GEMCOM1Xml As XmlElement = mXmlDocument.CreateElement("MACRO")
        GEMCOM1Xml.SetAttribute("COMMAND", "@GEMCOM1.")
        mDataNode.AddData(GEMCOM1Xml)

        Dim APCOMXml As XmlElement = mXmlDocument.CreateElement("MACRO")
        APCOMXml.SetAttribute("COMMAND", "@APCOM.")
        mDataNode.AddData(APCOMXml)

        If mIdentificationNode.IsMain Then
            Dim ISXPPD06Xml As XmlElement = mXmlDocument.CreateElement("INCLUDE")
            ISXPPD06Xml.SetAttribute("COPYBOOKNAME", "ISXPPD06")
            mDataNode.AddData(ISXPPD06Xml)
        Else
            'SUB专用指针区域
            Dim MIBXXXPTRXML As XmlElement = mXmlDocument.CreateElement("SINGLEDATA")
            MIBXXXPTRXML.SetAttribute("NAME", "MIB#PTR")
            MIBXXXPTRXML.SetAttribute("TYPE", "PTR")
            mDataNode.AddData(MIBXXXPTRXML)

            Dim WPRMXXXSVPTRXML As XmlElement = mXmlDocument.CreateElement("SINGLEDATA")
            WPRMXXXSVPTRXML.SetAttribute("NAME", "WPRM#SVPTR")
            WPRMXXXSVPTRXML.SetAttribute("TYPE", "PTR")
            mDataNode.AddData(WPRMXXXSVPTRXML)

            Dim XXXPRMXXXSVPTRXML As XmlElement = mXmlDocument.CreateElement("SINGLEDATA")
            XXXPRMXXXSVPTRXML.SetAttribute("NAME", "#PRM#SVPTR")
            XXXPRMXXXSVPTRXML.SetAttribute("TYPE", "PTR")
            mDataNode.AddData(XXXPRMXXXSVPTRXML)

            Dim ISXPPD06Xml As XmlElement = mXmlDocument.CreateElement("INCLUDE")
            ISXPPD06Xml.SetAttribute("COPYBOOKNAME", "ISXPPD06")
            mDataNode.AddData(ISXPPD06Xml)

            Dim MSXXXPRMXml As XmlElement = mXmlDocument.CreateElement("SET")
            MSXXXPRMXml.SetAttribute("SOURCE", "ADDRESS OF MS#PRM")
            MSXXXPRMXml.SetAttribute("TARGET", "PARM#PTR")
            mDataNode.ActionList.AddOperation(MSXXXPRMXml)

            Dim WTBPRMXml As XmlElement = mXmlDocument.CreateElement("SET")
            WTBPRMXml.SetAttribute("SOURCE", "ADDRESS OF WTBPRM")
            WTBPRMXml.SetAttribute("TARGET", "PRM#TBLP")
            mDataNode.ActionList.AddOperation(WTBPRMXml)
        End If
    End Sub
#End Region
#Region "程序调整"
    ''' <summary>
    ''' JRI的Macro转换
    ''' </summary>
    ''' <param name="mOprate"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Convert2Cobol(ByVal mOprate As XmlElement) As List(Of String)
        Dim strLine As String = String.Empty
        Dim strRtn As New List(Of String)
        strLine = GetSpace() & "@MARCO:" & mOprate.GetAttribute("COMMAND")
        strRtn.Add(strLine)
        Dim strMacro As String = mOprate.GetAttribute("COMMAND")
        Dim strMacroName As String
        If InStr(strMacro, "(") > 0 Then
            strMacroName = Mid(strMacro, 2, InStr(strMacro, "(") - 2)
        Else
            strMacroName = Mid(strMacro, 2)
        End If
        Select Case strMacroName
            Case "ICALL"
                strRtn = GetICALL(strMacro)
            Case "CALL"
                strRtn = GetCALL(strMacro)
        End Select
        Return strRtn
    End Function
#End Region
#Region "转换"
    ''' <summary>
    ''' ICALL
    ''' </summary>
    ''' <param name="strCommand"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetICALL(ByVal strCommand As String) As List(Of String)
        Dim strRtn As New List(Of String)
        '改写为PERFORM，注意，里面的过程名称都需要改写
        '原来过程名称是 AAA 的时候，先用 MODIFYIEDVAR改写，然后加上-RTN.
        '参数已经在Justfiy里面调整完毕，添加了MOVE语句
        Dim SubSectionName As String = GetAttr("#ICALL", GetSyntaxToken(strCommand)).Split(",")(0)
        strRtn.Add(GetSpace() & "PERFORM" & Space(Indent) & clsJustifyStatement.ModifySectionName(SubSectionName, False) & IIf(Deepth = 1, ".", String.Empty))
        Return strRtn
    End Function
    ''' <summary>
    ''' CALL
    ''' </summary>
    ''' <param name="strCommand"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetCALL(ByVal strCommand As String) As List(Of String)
        Dim strRtn As New List(Of String)
        Dim strCommandArray As String() = GetAttr("#CALL", GetSyntaxToken(strCommand)).Split(",")
        Dim ProgIDName As String = strCommandArray(0)
        If strCommandArray.Length > 1 Then
            Dim strTemp As String = GetSpace() & "CALL" & Space(Indent) & ProgIDName & Space(Indent) & "USING" & Space(Indent)
            strRtn.Add(strTemp & clsJustifyStatement.LastModifyVarName(strCommandArray(1)))
            For i As Byte = 2 To strCommandArray.Length - 1
                strRtn.Add(Space(strTemp.Length) & clsJustifyStatement.LastModifyVarName(strCommandArray(i)))
            Next
        Else
            strRtn.Add(GetSpace() & "CALL" & Space(Indent) & ProgIDName)
        End If
        If Deepth = 1 Then
            strRtn.Item(strRtn.Count - 1) &= "."
        End If
        Return strRtn
    End Function
#End Region
#Region "调整"
    Public Shared Sub JustifyCALL(ByVal CallNode As XmlElement, ByVal PreProcess As clsStatementBlock, ByVal AfterProcess As clsStatementBlock)
        Dim strCommand As String = CallNode.GetAttribute("COMMAND")
        Dim strCommandArray As String() = GetAttr("#ICALL", GetSyntaxToken(strCommand)).Split(",")
        Dim SectionName As String = strCommandArray(0)
        If strCommandArray.Length > 1 Then
            '寻找过程名称
            Dim ParaList As New List(Of String)
            For Each subSec As SectionNode In mProcedureNode.SectionList
                If subSec.SectionName = SectionName Then
                    ParaList = subSec.ParaList
                End If
            Next
            If ParaList.Count + 1 = strCommandArray.Length Then
                '改参数为MOVE语句
                For i As Byte = 1 To strCommandArray.Length - 1
                    Dim MoveXml As XmlElement = mXmlDocument.CreateElement("MOVE")
                    MoveXml.SetAttribute("TARGET", ParaList(i - 1))
                    MoveXml.SetAttribute("SOURCE", strCommandArray(i))
                    PreProcess.AddOperation(MoveXml)
                Next
                '删除参数
                CallNode.SetAttribute("COMMAND", "#ICALL(" & SectionName & ")")
            Else
                '参数匹配错误
                LogWriter.WriteLine("*ERROR:PARAMETER NOT MATCH")
            End If
        End If
    End Sub
#End Region
End Class
